package com.kevis.zhunblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kevis.zhunblog.core.dto.PageDomain;
import com.kevis.zhunblog.core.dto.query.CommonClassQuery;
import com.kevis.zhunblog.core.entities.ClassArticleEntity;
import com.kevis.zhunblog.mapper.ClassArticleMapper;
import com.kevis.zhunblog.service.ClassArticleService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;


/**
 * 文章分类
 */
@CacheConfig(cacheNames={"ClassArticleServiceImpl"})
@Service
public class ClassArticleServiceImpl extends CommonClassServiceImpl<ClassArticleMapper, ClassArticleEntity>
        //extends ServiceImpl<ClassArticleMapper, ClassArticleEntity>
        implements ClassArticleService{

    @Resource
    private ClassArticleMapper classArticleMapper;

    @Autowired
    private SqlSession sqlSession;

    @Autowired
    CacheManager cacheManager;

    /**
     * 分类的分页查询
     * @param query
     * @param pageDomain
     * @return
     */
    //@Cacheable(value = "cachename",key = "'hh-' + #root.args[0] + #root.args[1]")
    @Cacheable(keyGenerator = "selfKeyGenerate")
    public PageInfo<ClassArticleEntity> getPageList(CommonClassQuery query, PageDomain pageDomain){
        LambdaQueryWrapper<ClassArticleEntity> wrapper = new LambdaQueryWrapper<>();
        System.out.println("============="+cacheManager);
        if(query != null){
//            String SearchTimeBegin = "";
//            String SearchTimeEnd = "";
//            if(StringUtils.hasLength(query.getSearchTimeBegin()))
//                SearchTimeBegin = query.getSearchTimeBegin() + " 00:00:00";
//            if(StringUtils.hasLength(query.getSearchTimeEnd()))
//                SearchTimeEnd = query.getSearchTimeEnd() + " 23:59:59";
            switch (query.getSearchTimeType())
            {
                case 0:
                    //if(StringUtils.hasLength(SearchTimeBegin))
                    //    wrapper.ge(ClassArticleEntity::getCreateTime,SearchTimeBegin);
                    //if(StringUtils.hasLength(SearchTimeEnd))
                    //    wrapper.le(ClassArticleEntity::getCreateTime,SearchTimeEnd);
                    wrapper.ge(ClassArticleEntity::getCreateTime,query.getSearchTimeBegin());
                    wrapper.le(ClassArticleEntity::getCreateTime,query.getSearchTimeEnd());
                    break;
                case 1:
//                    if(StringUtils.hasLength(SearchTimeBegin))
//                        wrapper.ge(ClassArticleEntity::getUpdateTime,SearchTimeBegin);
//                    if(StringUtils.hasLength(SearchTimeEnd))
//                        wrapper.le(ClassArticleEntity::getUpdateTime,SearchTimeEnd);
                    wrapper.ge(ClassArticleEntity::getUpdateTime,query.getSearchTimeBegin());
                    wrapper.le(ClassArticleEntity::getUpdateTime,query.getSearchTimeEnd());
                    break;
            }
            if(StringUtils.hasLength(query.getSearchName()) && StringUtils.hasLength(query.getSearchValue()))
            {
                switch (query.getSearchName())
                {
                    case "title":
                        wrapper.like(ClassArticleEntity::getTitle,query.getSearchValue());
                        break;
                    case "id":
                        wrapper.eq(ClassArticleEntity::getId,query.getSearchValue());
                        break;
                }
            }
            if(query.getSearchParentId() >= 0){
                wrapper.eq(ClassArticleEntity::getParentId,query.getSearchParentId());
            }
            if(null != query.getSearchStatus() && query.getSearchStatus() != 9999){
                wrapper.eq(ClassArticleEntity::getStatus,query.getSearchStatus());
            }
        }
        //IPage<Map<String, Object>> page12 = new Page<Map<String, Object>>();
        //IPage iPage = this.pageMaps(page12, wrapper);
        //IPage<CommonClassQuery> page = new Page<CommonClassQuery>();
        //其中一种用法
        //RowBounds rowBounds = new RowBounds(2, 2);
        //List<ClassArticleEntity> list1 = sqlSession.selectList("com.kevis.zhunblog.mapper.ClassArticleMapper.getC1", null,rowBounds );

        //baseMapper.selectPage(page, wrapper);
        //PageHelper 为什么一定要用这个插件呢？因为默认的分页只支持实体分页，不支持本身的实体类，也就是自定义实体
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        List<ClassArticleEntity> list = super.list(wrapper);
        return new PageInfo<>(list);

    }
//    @Override
//    public List<ClassArticleEntity> listData(ClassArticleEntity c) {
//        LambdaQueryWrapper<ClassArticleEntity> wrapper = new LambdaQueryWrapper<ClassArticleEntity>(c);
//        wrapper.eq(ClassArticleEntity::getStatus,0);
//        if(c.getParentId() != null)
//            wrapper.eq(ClassArticleEntity::getParentId,c.getParentId());
//        return baseMapper.selectList(wrapper);
//        // return null;
//    }

    /**
     * Describe: 根据 parentId 查询权限 需要实时的数据，不能缓存
     * Param: parentId
     * Return: 操作结果
     */
    @Override
    public List<ClassArticleEntity> selectByParentId(Integer parentId) {
        LambdaQueryWrapper<ClassArticleEntity> wrapper = new LambdaQueryWrapper<>();
        if(parentId == null)parentId=0;
        wrapper.eq(ClassArticleEntity::getParentId,parentId);
        return baseMapper.selectList(wrapper);
    }

    /**
     * Describe: 根据 ID 删除信息
     * Param: id
     * Return: 执行结果
     * rollbackFor: 默认只在遇到运行时异常和Error时才会回滚，非运行时异常不回滚
     * 而rollbackFor属性可以解决这个问题，rollbackFor = Exception.class表示Exception及其子类的异常都会触发回滚，同时不影响Error的回滚。
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean remove(String id) {
        baseMapper.deleteById(id);
        return true;
    }

    @Caching(
            evict = {
                    @CacheEvict(value = "ClassArticleServiceImpl",allEntries = true),
                    @CacheEvict(value = "t_class_article",allEntries = true),
                    //@CacheEvict(value = "t_class_article",key = "'GetListAll'",condition = ""),
                    @CacheEvict(value = "commonClass",key = "'t_class_article'",condition = "",allEntries = true)
            }
    )
    public void clearCache(String cacheName,String chaheKey)
    {

    }
}



